<template>
  <div class="login_right">
    <div class="login_title">
      登录你的毕设账户
      <div class="regisiter">
        没有账户？<span @click="goRegister">免费注册</span>
      </div>
    </div>
    <el-form :rules="rules" ref="formData" :model="formData">
      <div class="input_box">
        <el-form-item prop="userEmail">
          <el-input
            v-model="formData.userEmail"
            class="user_email"
            placeholder="请输入您的邮箱"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="formData.password"
            class="user_password"
            placeholder="请输入您的密码"
            type="password"
            show-password
          ></el-input>
        </el-form-item>
      </div>
      <div class="login_btn">
        <el-button class="btn" type="primary" @click="login('formData')"
          >登录</el-button
        >
      </div>
    </el-form>
  </div>
</template>

<script>
import { checkEmail } from "@/utils/user.utils";
import { userLogin, getUserInfo } from "@/api/api";
export default {
  data() {
    const validatorUserEmail = (rule, value, callback) => {
      if (!value.trim()) callback(new Error("请输入您的邮箱账号"));
      if (!checkEmail(value)) callback(new Error("请输入正确的邮箱格式"));
      callback();
    };
    const validatorPassword = (rule, value, callback) => {
      if (!value.trim()) callback(new Error("请输入您的密码"));
      callback();
    };
    return {
      formData: { userEmail: "", password: "" },
      rules: {
        userEmail: [
          { required: true, validator: validatorUserEmail, trigger: "blur" },
        ],
        password: [
          { required: true, validator: validatorPassword, trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    goRegister() {
      this.$router.push("/register");
    },
    // 点击登录按钮
    login(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          const { formData } = this;
          userLogin(formData).then((res) => {
            const token = res.data.data.token;
            this.$store.commit("setToken", token);
            getUserInfo().then((res) => {
              console.log(res);
              console.log(res.data.data);
              this.$store.commit("setUser", res.data.data);
              this.$router.push("/home");
            });
          });
        }
        return;
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.login_right {
  transform: translate(0px, 0px);
  opacity: 1;
  visibility: inherit;
  overflow: hidden;
  background: #fff;
  padding: 50px 50px;
  border-radius: 20px;
  .login_title {
    font-size: 36px;
    font-weight: 900;
    .regisiter {
      font-size: 16px;
      font-weight: 400;
      opacity: 0.7;
      span {
        color: #009dff;
        cursor: pointer;
      }
    }
  }
  .input_box {
    border: 1px solid #fff;
    margin-top: 40px;
    .user_email {
    }
  }
  .btn {
    margin-top: 20px;
    width: 100%;
  }
}
</style>